<html>
<head>
<title>M.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #cf8e6d;}
.s1 { color: #bcbec4;}
.s2 { color: #bcbec4;}
.s3 { color: #6aab73;}
.s4 { color: #7a7e85;}
.s5 { color: #2aacb8;}
</style>
</head>
<body bgcolor="#1e1f22">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
M.java</font>
</center></td></tr></table>
<pre><span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">io</span><span class="s2">.</span><span class="s1">BufferedReader</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">io</span><span class="s2">.</span><span class="s1">BufferedWriter</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">io</span><span class="s2">.</span><span class="s1">FileReader</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">io</span><span class="s2">.</span><span class="s1">FileWriter</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">io</span><span class="s2">.</span><span class="s1">IOException</span><span class="s2">;</span>

<span class="s0">public </span><span class="s1">Aclass AveragePriceCalculator </span><span class="s2">{</span>
    <span class="s0">public static void </span><span class="s1">main</span><span class="s2">(</span><span class="s1">String</span><span class="s2">[] </span><span class="s1">args</span><span class="s2">) {</span>
        <span class="s1">String filePath </span><span class="s2">= </span><span class="s3">&quot;path/to/your/file.txt&quot;</span><span class="s2">; </span><span class="s4">// 替换为实际文件路径</span>
        <span class="s1">calculateAndAppendAveragePrice</span><span class="s2">(</span><span class="s1">filePath</span><span class="s2">);</span>
    <span class="s2">}</span>

    <span class="s0">private static void </span><span class="s1">calculateAndAppendAveragePrice</span><span class="s2">(</span><span class="s1">String filePath</span><span class="s2">) {</span>
        <span class="s0">int </span><span class="s1">totalSum </span><span class="s2">= </span><span class="s5">0</span><span class="s2">;</span>
        <span class="s0">int </span><span class="s1">itemCount </span><span class="s2">= </span><span class="s5">0</span><span class="s2">;</span>
        <span class="s0">try </span><span class="s2">(</span><span class="s1">BufferedReader reader </span><span class="s2">= </span><span class="s0">new </span><span class="s1">BufferedReader</span><span class="s2">(</span><span class="s0">new </span><span class="s1">FileReader</span><span class="s2">(</span><span class="s1">filePath</span><span class="s2">))) {</span>
            <span class="s1">String line</span><span class="s2">;</span>
            <span class="s0">while </span><span class="s2">((</span><span class="s1">line </span><span class="s2">= </span><span class="s1">reader</span><span class="s2">.</span><span class="s1">readLine</span><span class="s2">()) != </span><span class="s0">null</span><span class="s2">) {</span>
                <span class="s0">if </span><span class="s2">(</span><span class="s1">line</span><span class="s2">.</span><span class="s1">contains</span><span class="s2">(</span><span class="s3">&quot;元/台&quot;</span><span class="s2">)) {</span>
                    <span class="s1">String</span><span class="s2">[] </span><span class="s1">parts </span><span class="s2">= </span><span class="s1">line</span><span class="s2">.</span><span class="s1">split</span><span class="s2">(</span><span class="s3">&quot;元/台&quot;</span><span class="s2">);</span>
                    <span class="s0">int </span><span class="s1">price </span><span class="s2">= </span><span class="s1">Integer</span><span class="s2">.</span><span class="s1">parseInt</span><span class="s2">(</span><span class="s1">parts</span><span class="s2">[</span><span class="s5">0</span><span class="s2">].</span><span class="s1">trim</span><span class="s2">().</span><span class="s1">split</span><span class="s2">(</span><span class="s3">&quot;，&quot;</span><span class="s2">)[</span><span class="s5">1</span><span class="s2">]);</span>
                    <span class="s1">totalSum </span><span class="s2">+= </span><span class="s1">price</span><span class="s2">;</span>
                    <span class="s1">itemCount</span><span class="s2">++;</span>
                <span class="s2">}</span>
            <span class="s2">}</span>
        <span class="s2">} </span><span class="s0">catch </span><span class="s2">(</span><span class="s1">IOException e</span><span class="s2">) {</span>
            <span class="s1">e</span><span class="s2">.</span><span class="s1">printStackTrace</span><span class="s2">();</span>
        <span class="s2">}</span>

        <span class="s0">if </span><span class="s2">(</span><span class="s1">itemCount </span><span class="s2">&gt; </span><span class="s5">0</span><span class="s2">) {</span>
            <span class="s0">double </span><span class="s1">averagePrice </span><span class="s2">= </span><span class="s1">totalSum </span><span class="s2">/ (</span><span class="s0">double</span><span class="s2">) </span><span class="s1">itemCount</span><span class="s2">;</span>
            <span class="s1">appendAveragePriceToFile</span><span class="s2">(</span><span class="s1">filePath</span><span class="s2">, </span><span class="s1">averagePrice</span><span class="s2">);</span>
        <span class="s2">}</span>
    <span class="s2">}</span>

    <span class="s0">private static void </span><span class="s1">appendAveragePriceToFile</span><span class="s2">(</span><span class="s1">String filePath</span><span class="s2">, </span><span class="s0">double </span><span class="s1">averagePrice</span><span class="s2">) {</span>
        <span class="s0">try </span><span class="s2">(</span><span class="s1">BufferedWriter writer </span><span class="s2">= </span><span class="s0">new </span><span class="s1">BufferedWriter</span><span class="s2">(</span><span class="s0">new </span><span class="s1">FileWriter</span><span class="s2">(</span><span class="s1">filePath</span><span class="s2">, </span><span class="s0">true</span><span class="s2">))) {</span>
            <span class="s1">writer</span><span class="s2">.</span><span class="s1">newLine</span><span class="s2">();</span>
            <span class="s1">writer</span><span class="s2">.</span><span class="s1">write</span><span class="s2">(</span><span class="s3">&quot;平均价格: &quot; </span><span class="s2">+ </span><span class="s1">averagePrice </span><span class="s2">+ </span><span class="s3">&quot;元/台&quot;</span><span class="s2">);</span>
        <span class="s2">} </span><span class="s0">catch </span><span class="s2">(</span><span class="s1">IOException e</span><span class="s2">) {</span>
            <span class="s1">e</span><span class="s2">.</span><span class="s1">printStackTrace</span><span class="s2">();</span>
        <span class="s2">}</span>
    <span class="s2">}</span>
<span class="s2">}</span>
</pre>
</body>
</html>